import re
from sqlalchemy import ForeignKey, UniqueConstraint, Column
from sqlalchemy.sql.sqltypes import String, Integer, Date, Text
from app.core.base_model import BaseModel


pattern = re.compile(r'<[^>]+>', re.S)


def gen_brief(content, size: int = 500):
    if content is not None and len(content.strip()) > 0:
        brief = pattern.sub('', content.strip())
        return brief[:size].strip()
    else:
        return ""


class Article(BaseModel):
    __tablename__ = 't_article'
    title = Column(String(64))
    content = Column(Text, nullable=False)
    tags = Column(String(64), nullable=True)
    brief = Column(String(1024))

    def __init__(self, title, content, tags, brief):
        self.title = title
        self.content = content
        self.tags = tags
        self.brief = brief if brief is not None else gen_brief(self.content)

    def refresh_brief(self):
        self.brief = gen_brief(self.content)

    def __repr__(self):
        return '<Art %s>' % self.id
